  var response
  var JSONobject
  
  function empty_next_combo(id_val){
  switch (id_val){
  case "company" :ClearOptionsFast("region");ClearOptionsFast("branch");break;
  case "region" :ClearOptionsFast("branch");break;
  case "branch" :break;
  default :break;
  }
  }
  
  function ajax_request(id_val){
  	
	  	var selected_index = document.getElementById(id_val).selectedIndex;
	  	if (selected_index!=0){
	  	  	var selected_option_text = document.getElementById(id_val).options[selected_index].value;
	  		var JSONobject = new Object();
			JSONobject.box = id_val;
			JSONobject.value = selected_option_text;
			var JSONstring = JSON.stringify(JSONobject);
			new Ajax.Request('comboFeeder.php', { method: 'post', contentType:'application/json', evalJSON: 'force', asynchronous:true, postBody: JSONstring, onComplete: function(transport){ajax_response(transport.responseText) || "vipin: no response text";}});
			empty_next_combo(id_val);
		}
  }
  
  function find_size(myArray){
  	var element_count = 0;
	for (id in myArray){element_count++; }
    return element_count;
	}
	
  function addOption(id,text,value,sel)
	{
		var optn = document.createElement("OPTION");
		selectbox=window.document.getElementById(id)
		if (sel == true) optn.selected = true;
		optn.text = text;
		optn.value = value;
		selectbox.options.add(optn);
	}
  	
  function ClearOptionsFast(id)
{//window.document.getElementById(id).innerHTML = "";
	var selectObj = window.document.getElementById(id);
	var selectParentNode = selectObj.parentNode;
	var newSelectObj = selectObj.cloneNode(false); // Make a shallow copy
	selectParentNode.replaceChild(newSelectObj, selectObj);
	//window.document.getElementById(id)["0"].selected =true;
	return newSelectObj;
}

  function ajax_response(JSONtext) {
  	//alert("Recieved : " + JSONobj);
	JSONobject = eval("(" + JSONtext + ")");
  	switch (JSONobject.receiver){
  		case "regionbox" : 	var len=find_size(JSONobject.ret_set);
  							addOption("region","",0,true);
  							for(i=1;i<=len;i++){
  								addOption("region",JSONobject.ret_set[i].name,JSONobject.ret_set[i].id,false);
  								} 
  							break;
  		case "branchbox" :  var len=find_size(JSONobject.ret_set);
  							addOption("branch","",0,true);
  							for(i=1;i<=len;i++){
  								addOption("branch",JSONobject.ret_set[i].name,JSONobject.ret_set[i].id,false);
  								}
  							break;
  		default : alert("Ajax Error: No Data Recieved");break;
  	}
  }